var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '', //名称
    address: '', //省市区
    province: '',
    city: '',
    area: '',
    detailAddress: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    let info = JSON.parse(options.info);
    this.setData({
      name: info.name,
      address: info.province + ' ' + info.city + ' ' + info.area,
      detailAddress: info.address,
      id: info.id
    });
  },
  bindName(e) {
    this.setData({
      name: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  onAddress(e) {
    this.setData({
      detailAddress: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  chooseAddress() {
    wx.navigateTo({
      url: '../mine/mapLocation/searchMapLocation/searchMapLocation'
    });
  },
  bindFormSubmit() {
    var that = this;
    var name = that.data.name;
    var address = that.data.address;
    var detailAddress = that.data.detailAddress;
    var province = that.data.address.split(' ')[0];
    var city = that.data.address.split(' ')[1];
    var area = that.data.address.split(' ')[2];
    if (!name) {
      wx.showToast({
        title: '厂名不能为空',
        icon: 'none'
      });
      return false;
    }
    if (!address || !detailAddress) {
      wx.showToast({
        title: '地址不能为空',
        icon: 'none'
      });
      return false;
    } else {
      wx.showLoading({ title: '提交中' });
      that.setData({ buthiddenSubmit: true });
      wx.request({
        method: 'POST',
        url: app.preUrl + 'updateSiteFactory',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          id: that.data.id,
          userId: app.globalData.userId,
          name: name,
          province: province,
          city: city,
          area: area,
          address: detailAddress
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.code == 100) {
            wx.showToast({
              title: '绑定成功',
              icon: 'success',
              duration: 1000
            });
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              });
            }, 1500);
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
            that.setData({ buthiddenSubmit: false });
          }
        },
        fail: function(error) {
          that.setData({ buthiddenSubmit: false });
        }
      });
    }
  }
});
